package net.csdn.business.notify.api.action;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.notify.api.config.ApiConfigData;
import net.csdn.business.notify.common.enums.NotifyType;
import net.csdn.business.notify.common.enums.ProcessStatusEnum;
import net.csdn.business.notify.common.model.vo.NotifyBusinessVO;
import net.csdn.business.notify.common.model.vo.NotifyResponseVO;
import net.csdn.business.notify.common.pipeline.BusinessProcess;
import net.csdn.business.notify.common.pipeline.ProcessContext;
import net.csdn.business.notify.common.pipeline.domain.SendTaskModel;
import net.csdn.business.notify.common.pipeline.param.MessageParam;
import net.csdn.business.notify.common.pipeline.param.SendRemindParam;
import net.csdn.business.notify.api.service.INotifyBusinessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @ClassName SendPreCheckAction
 * @Description 发送消息：前置公共参数校验
 * @Author zhangyl
 * @Date 2023/11/29 16:49
 */
@Slf4j
@Service
public class RemindPreCheckAction implements BusinessProcess<SendTaskModel> {

    @Autowired
    private ApiConfigData apiConfigData;

    @Autowired
    private INotifyBusinessService notifyBusinessService;

    @Override
    public void process(ProcessContext<SendTaskModel> context) {
        NotifyResponseVO response=context.getResponse();
        Map<String,String> failMap=response.getFail();
        SendTaskModel sendTaskModel = context.getProcessModel();
        List<MessageParam> messageParamList = sendTaskModel.getMessageParamList();
        if(messageParamList==null||messageParamList.size()==0){
            response.setStatus(ProcessStatusEnum.PARAMETERS_IS_NULL);
            context.setNeedBreak(true);
            return;
        }
        if(messageParamList.size() > apiConfigData.getBatchLimit()){
            response.setStatus(ProcessStatusEnum.EXCEED_BATCH_NUM_LIMIT);
            context.setNeedBreak(true);
            return;
        }
        //循环遍历,不符的参数移除到失败集合里
        List<MessageParam> messageParamAfter=new ArrayList<>();
        for (MessageParam messageParam:messageParamList){
            SendRemindParam sendRemindParam =(SendRemindParam)messageParam;
            String busCode = sendRemindParam.getBusCode();
            String batchNum= sendRemindParam.getBatchNum();
            //查询业务编码信息
            NotifyBusinessVO notifyBusinessVO=notifyBusinessService.selectNotifyBusinessByCode(busCode,1);
            if(notifyBusinessVO==null){
                log.info("--remind pre check action not found business batchNum:{}",batchNum);
                failMap.put(batchNum,ProcessStatusEnum.BUS_CODE_NOT_FOUND.getMsg());
                continue;
            }
            String targetType=notifyBusinessVO.getTargetType();
            String act=notifyBusinessVO.getAct();
            Integer notifyType=notifyBusinessVO.getNotifyType();
            sendRemindParam.setAct(act);
            sendRemindParam.setBusId(notifyBusinessVO.getId());
            sendRemindParam.setTargetType(targetType);
            sendRemindParam.setNotifyType(notifyType);
            //如果是营销类通知的业务，此接口不支持
            if(notifyType.equals(NotifyType.DIRECT)){
                failMap.put(batchNum,String.format(ProcessStatusEnum.BUS_CODE_NOT_ALLOW.getMsg(), "url"));
                continue;
            }
            if(notifyType.equals(NotifyType.REMIND)){
                String url=sendRemindParam.getUrl();
                if(StringUtils.isBlank(url)){
                    failMap.put(batchNum,String.format(ProcessStatusEnum.CLIENT_BAD_PARAMETERS.getMsg(), "url"));
                    continue;
                }
                String targetId=sendRemindParam.getTargetId();
                if(StringUtils.isBlank(targetId)){
                    failMap.put(batchNum,String.format(ProcessStatusEnum.CLIENT_BAD_PARAMETERS.getMsg(), "targetId"));
                    continue;
                }
                JSONObject nameSpace=sendRemindParam.getNameSpace();
                if(nameSpace==null){
                    failMap.put(batchNum,String.format(ProcessStatusEnum.CLIENT_BAD_PARAMETERS.getMsg(), "nameSpace"));
                    continue;
                }
            }
            Set<String> receiver=messageParam.getReceiver();
            //1、接收者为空则，终止执行
            if(receiver==null||receiver.size()==0){
                failMap.put(batchNum,String.format(ProcessStatusEnum.CLIENT_BAD_PARAMETERS.getMsg(), "receiver"));
                continue;
            }
            // 2. 过滤 receiver 大于100的请求
            if(messageParamList.size() > apiConfigData.getBatchLimit()){
                failMap.put(batchNum,String.format(ProcessStatusEnum.EXCEED_RECEIVER_NUM_LIMIT.getMsg(), "receiver"));
                continue;
            }
            messageParamAfter.add(messageParam);
        }
        if(messageParamAfter.size()==0){
            response.setStatus(ProcessStatusEnum.FAIL);
            context.setNeedBreak(true);
            return;
        }
        sendTaskModel.setMessageParamList(messageParamAfter);
    }
}
